using AdminApiTemplate.Api.Controllers.BaseControllers;
using AdminApiTemplate.Api.Enums;
using AdminApiTemplate.Api.Helpers;
using AdminApiTemplate.Api.ViewModels.AdminUser;
using AdminApiTemplate.Services.AdminUser;
using AdminApiTemplate.Services.AdminUser.Models;
using AdminApiTemplate.ThirdParty.Redis;
using Microsoft.AspNetCore.Mvc;

namespace AdminApiTemplate.Api.Controllers.AdminControllers
{
    /// <summary>
    /// 后台账号管理（需要登录）
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class AdminUserController : PermissionRequiredController
    {
        /// <summary>
        /// 当前后台账号信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Current")]
        public IActionResult Current()
        {
            return new WebHelper(HttpContext, null, false).Execute(SysUserTypeEnum.AdminUser, AUID.ToString(), () =>
            {
                var response = new CurrentServiceModel
                {
                    Menus = new List<MenuItem>()
                };
                var id = $"admin:{AUID}";
                var adminUser = RedisHelper.Get<CachedAdminUserDto>(id);
                if (adminUser != null)
                {
                    response.AUID = adminUser.AUID;
                    response.WorkWx = adminUser.WorkWx;
                    response.ChineseName = adminUser.ChineseName;
                    response.EnglishName = adminUser.EnglishName;
                    response.Permissions = $",{string.Join(",", adminUser.Permissions)},";
                    if (adminUser.Menus != null)
                    {
                        response.Menus = adminUser.Menus;
                    }
                }
                return response;
            });
        }

        /// <summary>
        /// 登录记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("LoginLog")]
        public IActionResult LoginLog(LoginLogRequest request)
        {
            return GetPagedList<LoginLogRequest, LoginLogItem>(request, "login_log", "*", "Id");
        }
    }
}
